package exttable.testversioning.testversionrunner;

import exttable.testversioning.ButtonHandler;
import exttable.testversioning.ControlNodes;
import exttable.testversioning.tablemodel.AnalysisPhaseModel;
import exttable.view.ViewNodes;
import utils.Localisation;

import javax.swing.*;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jun 7, 2010
 * Time: 4:50:04 PM
 * To change this template use File | Settings | File Templates.
 */
public class TestVersionView {
    private JFrame frame;
//    private JLabel phaseLabel;
    private JComboBox phaseList;
    private JTable unitTable;
    private TableModel phasesModel;
    private TableModel analysisVersionModel;
    private JScrollPane tableScrollFill;
    private SpringLayout layout = new SpringLayout();
//    private JLabel infoSelectAnalysis;
//    private JButton fillPhasesWithAnalysis;
    private JButton saveAnalysisInPhases;
    private JTree controlTree;
    private TestVersionModel model;

    public TestVersionView(TestVersionModel model) {
        this.model = model;
        try {
            init();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void init() throws Exception {
        frame = new JFrame(Localisation.getMessage("title.versioning"));
        frame.setLayout(layout);

//        phaseLabel = new JLabel(Localisation.getMessage("info.selectphase"));
//        phaseLabel.setVisible(false);
//        infoSelectAnalysis = new JLabel(Localisation.getMessage("info.selectanalysisversion"));
//        infoSelectAnalysis.setVisible(false);
        phaseList = new JComboBox(model.getPhases());
        phaseList.setVisible(false);
        unitTable = new JTable();
        unitTable.setRowHeight(20);
        unitTable.setRowMargin(2);
        phasesModel = model.getTestVersionTablePhase();
        analysisVersionModel = model.getTestVersionTable();
        controlTree = new JTree(initTree());
        controlTree.setPreferredSize(new Dimension(250, 400));
//        fillPhasesWithAnalysis = new JButton(Localisation.getMessage("form.fillPhasesWithAnalysis"));
//        fillPhasesWithAnalysis.setVisible(false);
        saveAnalysisInPhases = new JButton(Localisation.getMessage("form.saveresult"));
        saveAnalysisInPhases.setVisible(false);
        tableScrollFill = new JScrollPane(unitTable);

        frame.add(tableScrollFill);
//        frame.add(infoSelectAnalysis);
//        frame.add(phaseLabel);
        frame.add(phaseList);
//        frame.add(fillPhasesWithAnalysis);
        frame.add(saveAnalysisInPhases);
        frame.add(controlTree);
        organiseLayout();
        frame.setSize(750, 650);
        frame.setVisible(true);
        frame.setLocation(300, 100);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void viewAnalysisInPhases() {
        try {
            unitTable.setModel(new AnalysisPhaseModel());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void displayAnalysisVersionCreateList() {
        unitTable.setModel(phasesModel);
        unitTable.getColumnModel().getColumn(0).setCellEditor(new ButtonHandler(new JCheckBox(), TestVersionView.this));
        unitTable.getColumnModel().getColumn(0).setCellRenderer(new TestVersionsRenderer());
        if (unitTable.getColumnModel().getColumnCount() == 3) {
            unitTable.removeColumn(unitTable.getColumnModel().getColumn(1));
        }

    }

    public void displayAnalysisPhasesFillList() {
        unitTable.setModel(analysisVersionModel);
        unitTable.getColumnModel().getColumn(2).setCellRenderer(new TestVersionsRenderer());
//        unitTable.getColumnModel().getColumn(2).setCellEditor(new TestVersionsEditor());
//        unitTable.getColumnModel().getColumn(2).setCellEditor(new TestVersionsEditor());
    }

    private DefaultMutableTreeNode initTree() {
        ViewNodes root = new ViewNodes.Build().name(Localisation.getMessage("tree.controlpanel")).nodeType(ControlNodes.MAIN).build();
        DefaultMutableTreeNode category = new DefaultMutableTreeNode(root);
//        category.add(initPhases());
        category.add(initAnalysis());
        return category;
    }

//    private DefaultMutableTreeNode initPhases() {
//        ViewNodes root = new ViewNodes.Build().name(Localisation.getMessage("tree.phase")).nodeType(ControlNodes.PHASE).build();
//        ViewNodes view = new ViewNodes.Build().name(Localisation.getMessage("tree.viewphase")).nodeType(ControlNodes.VIEW_PHASE).build();
//        ViewNodes create = new ViewNodes.Build().name(Localisation.getMessage("tree.fillphase")).nodeType(ControlNodes.CREATE_PHASE).build();
//        return getNodes(root, view, create);
//    }

    private DefaultMutableTreeNode initAnalysis() {
        ViewNodes root = new ViewNodes.Build().name(Localisation.getMessage("tree.analysis")).nodeType(ControlNodes.ANALYSIS).build();
//        ViewNodes view = new ViewNodes.Build().name(Localisation.getMessage("tree.viewanalysis")).nodeType(ControlNodes.VIEW_ANALYSIS).build();
//        ViewNodes create = new ViewNodes.Build().name(Localisation.getMessage("tree.creaateanalysis")).nodeType(ControlNodes.CREATE_ANALYSIS).build();
        ViewNodes createAnalysisVersion = new ViewNodes.Build().name(Localisation.getMessage("tree.createanalysisversion")).nodeType(ControlNodes.CREATE_ANALYSIS_VERSION).build();
        return getNodes(root,  createAnalysisVersion);
    }


    private DefaultMutableTreeNode getNodes(ViewNodes... nodes) {
        DefaultMutableTreeNode toReturn = new DefaultMutableTreeNode(nodes[0]);
        for (int i = 1; i < nodes.length; i++) {
            DefaultMutableTreeNode dmView = new DefaultMutableTreeNode(nodes[i]);
            toReturn.add(dmView);
        }
        return toReturn;
    }


    public void organiseLayout() {
        layout.putConstraint(SpringLayout.WEST, controlTree, 5, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, controlTree, 5, SpringLayout.SOUTH, frame);

//        layout.putConstraint(SpringLayout.WEST, infoSelectAnalysis, 5, SpringLayout.EAST, controlTree);
//        layout.putConstraint(SpringLayout.NORTH, infoSelectAnalysis, 2, SpringLayout.SOUTH, frame);

        layout.putConstraint(SpringLayout.WEST, tableScrollFill, 10, SpringLayout.EAST, controlTree);
//        layout.putConstraint(SpringLayout.NORTH, tableScrollFill, 10, SpringLayout.SOUTH, infoSelectAnalysis);

//        layout.putConstraint(SpringLayout.WEST, phaseLabel, 10, SpringLayout.EAST, controlTree);
//        layout.putConstraint(SpringLayout.NORTH, phaseLabel, 5, SpringLayout.SOUTH, tableScrollFill);

        layout.putConstraint(SpringLayout.WEST, phaseList, 10, SpringLayout.EAST, controlTree);
//        layout.putConstraint(SpringLayout.NORTH, phaseList, 5, SpringLayout.SOUTH, phaseLabel);

//        layout.putConstraint(SpringLayout.WEST, fillPhasesWithAnalysis, 10, SpringLayout.EAST, controlTree);
//        layout.putConstraint(SpringLayout.NORTH, fillPhasesWithAnalysis, 10, SpringLayout.SOUTH, phaseList);

        layout.putConstraint(SpringLayout.WEST, saveAnalysisInPhases, 10, SpringLayout.EAST, controlTree);
        layout.putConstraint(SpringLayout.NORTH, saveAnalysisInPhases, 10, SpringLayout.SOUTH, phaseList);
    }

    public JTable getUnitTable() {
        return unitTable;
    }

    public JComboBox getPhaseList() {
        return phaseList;
    }

//    public JButton getFillPhasesWithAnalysis() {
//        return fillPhasesWithAnalysis;
//    }
//
//    public JButton getSaveAnalysisInPhases() {
//        return saveAnalysisInPhases;
//    }

//    public JLabel getInfoSelectAnalysis() {
//        return infoSelectAnalysis;
//    }

//    public JLabel getPhaseLabel() {
//        return phaseLabel;
//    }

    public JTree getControlTree() {
        return controlTree;
    }

//    void addFillAnalysisAction(ActionListener fill) {
//        fillPhasesWithAnalysis.addActionListener(fill);
//    }

    void addSaveAnalysisAction(ActionListener save) {
        saveAnalysisInPhases.addActionListener(save);
    }

    void addTreeSelection(TreeSelectionListener treeListener) {
        controlTree.addTreeSelectionListener(treeListener);
    }

}

